package com.code.demo.util;

import android.content.Context;
import android.content.pm.PackageInfo;

/**
 * Created by yangliguo on 2018/3/23.
 */

public class AppUtil {

    public static String getPackageName(Context context) {
        PackageInfo info;
        try {
            info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return info.packageName;
    }

    public static int getVersionCode(Context context) {
        PackageInfo info;
        try {
            info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return info.versionCode;
    }

    public static String getVersionName(Context context) {
        PackageInfo info;
        try {
            info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return info.versionName;
    }
}
